<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="名字"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-select
        v-model="listQuery.type"
        placeholder="类别"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @change="handleFilter"
      >
        <el-option label="其他" :value="1">其他</el-option>
        <el-option label="视频" :value="2">视频</el-option>
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        size="mini"
        style="margin-left:10px;"
        @click="handleFilter"
      >查询</el-button>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        size="mini"
        style="margin-left:10px;"
        @click="add"
      >新增</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoadding"
      :data="list"
      size="mini"
      border
      fit
      highlight-current-row
      style="width:100%"
      @sort-change="sortChange"
    >
      <el-table-column
        label="序号"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="原始名称"
        align="center"
        width="200"
      >
        <template slot-scope="{ row: { mediaFileOriginalName }}">
          <span>{{ mediaFileOriginalName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="类型"
        align="center"
        width="80"
      >
        <template slot-scope="{ row: { rtype }}">
          <el-tag :type="rtype==1?'warning':'success'">{{ rtype==1?'其他':'视频' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="访问地址"
        align="center"
        width="500"
      >
        <template slot-scope="{ row: { mediaUrl }}">
          <span>
            <a style="color:#909399;text-decoration: underline;" :href="mediaUrl" target="_blank">{{ mediaUrl }}</a>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="上传用户"
        align="center"
        width="120"
      >
        <template slot-scope="{ row: { createBy }}">
          <span>{{ createBy }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="上传时间"
        align="center"
        width="160"
      >
        <template slot-scope="{ row: { createTime }}">
          <el-tag>{{ createTime }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="114"
      >
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteData(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>=0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="handlePageList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { getResourcesList, deleteResource } from '@/api/resource'
export default {
  inject: ['reload'],
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    const generateData = _ => {
      const data = []
      for (let i = 1; i <= 15; i++) {
        data.push({
          key: i,
          label: `学号 ${i}`,
          disabled: i % 4 === 0
        })
      }
      return data
    }
    return {
      tableKey: 0,
      listLoadding: true,
      list: [],
      total: 0,
      listQuery: {
      },
      dialogFormVisible: false,
      data: generateData(),
      value: [1],
      value4: [1],
      renderFunc(h, option) {
        return <span>{ option.key } - { option.label }</span>
      }
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
  },
  methods: {
    parseQuery() {
      // 解析查询条件
      const liatQuery = {
        page: 1,
        pageSize: 10
      }
      // 合并查询条件
      this.listQuery = { ...liatQuery, ...this.listQuery }
    },
    getList() {
      this.listLoadding = true
      // 调用接口
      getResourcesList(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.content
        this.total = response.data.totalSize
      })
      this.listLoadding = false
    },
    handleFilter() {
      this.list = this.getList()
    },
    // 点击不同页时调用的方法
    handlePageList() {
      this.list = this.getList()
    },
    sortChange(data) {
      console.log('排序' + data)
    },
    handleChange(value, direction, movedKeys) {
      console.log(value, direction, movedKeys)
    },
    add() {
      this.$router.push('/course/resource')
    },
    deleteData(row) {
      this.$confirm('此操作将永久删除该资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用接口
        deleteResource(row.id).then(response => {
          this.reload()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>
.transfer-footer {
    margin-left: 20px;
    padding: 6px 5px;
  }
</style>
